package org.softmed.action.converter;

import org.softmed.action.description.ActionDescription;
import org.softmed.action.description.ActionStepDescription;
import org.softmed.action.description.StateManager;

import com.thoughtworks.xstream.XStream;

public class DefaultXStreamActionConverter implements
		ActionDescriptionXMLConverter {

	XStream xstream = new XStream();

	public DefaultXStreamActionConverter() {
		xstream.alias("action", ActionDescription.class);
		xstream.alias("step", ActionStepDescription.class);
		xstream.omitField(ActionStepDescription.class, "type");
		xstream.omitField(ActionStepDescription.class, "file");
		xstream.aliasField("steps", StateManager.class, "actionSteps");

		xstream.useAttributeFor(ActionStepDescription.class, "source");
		xstream.addImplicitCollection(StateManager.class, "actionSteps");
		// xstream
		// .addImplicitCollection(ActionStepDescription.class,
		// "parameters");
		xstream.aliasField("assert", ActionDescription.class,
		"assertion");		
		xstream.aliasField("preparation-failure", ActionDescription.class,
				"preparationFailure");
		xstream.aliasField("execution-success", ActionDescription.class,
				"executionSuccess");
		xstream.aliasField("execution-cancel", ActionDescription.class,
				"executionCancel");
		xstream.aliasField("execution-failure", ActionDescription.class,
				"executionFailure");

	}

	@Override
	public ActionDescription convertToActionDescription(String xml) {
		return (ActionDescription) xstream.fromXML(xml);
	}

	@Override
	public String convertToXML(ActionDescription action) {
		return xstream.toXML(action);
	}

}
